package com.webapp.model;

import java.sql.ResultSet;
import java.sql.Statement;

public class PostModel {
	
	private Statement stmt = DatabaseConnector.connect();
	
	/**
	 * Method that connects to the database and gathers the data for a specific post
	 * @param postId the ID for the desired post
	 * @return the Post that was requested
	 */
	public Post getPost(int postId){
		ResultSet rs;
		try{
			rs = stmt.executeQuery("SELECT * FROM posts WHERE Post_Id = '" +postId+ "'");
			if(rs.next() != false){
				Post post = new Post(postId, rs.getDate(2), rs.getString(3));
				post.setCommentCount(rs.getInt(4));
				post.setCircle(rs.getInt(5));
				post.setAuthor(rs.getInt(6));
				
				rs.close();
				stmt.close();
				return post;
			}
			else
				return null;
		}catch(Exception ex){return null;}
	}
	
	/**
	 * Method that connects to the database and inserts a new Post into the 'posts' table
	 * @param newPost the post to be added
	 * @return returns TRUE of successful, otherwise FALSE
	 */
	public boolean createPost(Post newPost){
		try{
			stmt.executeQuery("INSERT INTO posts (Post_Id, Date, Content, Comment_Count, Circle, Author) VALUES ('"+
					newPost.getPostId()+ "', '" +newPost.getDate()+ "', '" +newPost.getContent()+ "', '" +
					newPost.getCommentCount()+ ", '" +newPost.getCircle()+ "', '" +newPost.getAuthor()+ "')");
			stmt.close();
		}catch(Exception ex){return false;}
		
		return true;
	}
	
	/**
	 * Method that connects to the database, checks to see if a specific post exists already,
	 * and updates that post to the new desired post.
	 * @param updatedPost the new data which will update the previous post
	 * @return returns TRUE is successful, otherwise FALSE
	 */
	public boolean modifyPost(Post updatedPost){
		try{
			if((stmt.executeQuery("SELECT * FROM posts WHERE Post_Id = '" +updatedPost.getPostId()+ "'")).next()){
				stmt.executeUpdate("UPDATE posts SET Date = '" +updatedPost.getDate()+ "', Content = '" +
								updatedPost.getContent()+ "', Author = '" +updatedPost.getAuthor()+ 
								"' WHERE Post_Id = '" +updatedPost.getPostId()+ "'");
				stmt.close();
			}
			else
				return false;
			
		}catch(Exception ex){return false;}
		
		return true;
	}
	
	/**
	 * Method that connects to the database and removes an existing post
	 * @param postId the ID to the post that will be deleted
	 * @return returns TRUE if successful, otherwise false
	 */
	public boolean deletePost(int postId){
		try{
			stmt.executeQuery("DELETE FROM posts WHERE Post_Id = '" +postId+ "'");
			stmt.close();
		}catch(Exception ex){return false;}
		
		return true;
	}
	
	/**
	 * Method that connects to the database and inserts values for a specific post and user into the 
	 * 'like' table, showing a user likes a specific post
	 * @param postId the ID for the specific post
	 * @param userId the ID for the user who performed this action
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean likePost(int postId, int userId){
		try{
			stmt.executeQuery("INSERT INTO user_likes_post (User, Post) VALUES ('" +
					userId+ "', '" +postId+ "')");
			stmt.close();
		}catch(Exception ex){return false;}
		
		return true;
	}
	
	/**
	 * Method that connects to the database and deletes an existing value within the 'like'
	 * table, essentially showing the user no longer likes this post
	 * @param postId the ID for the specific post
	 * @param customerId the ID for the user who performed this action
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean unlikePost(int postId, int customerId){
		try{
			stmt.executeQuery("DELETE FROM user_likes_post WHERE Post = '" +postId+ "' AND User = '" +customerId+ "'");
			stmt.close();
		}catch(Exception ex){return false;}
		
		return true;
	}
}
